#include "analy/ui/ToolGui.h"

ToolWin::ToolWin(QMap<QString, QMap<QString, QString>> &_RGLRLSMAP_,
                 QSqlDatabase *_DATABASE_,
                 QMap<QString, QString> &_QSSMAP_)
    : NMainWindow()
    , ToolBranchBT()
    , ToolBranchCT()
    , ToolBranchRF()
    , RegisterSpToolsCount(3)
    {
     __RLMAP__ = &_RGLRLSMAP_;
     DATABASE = _DATABASE_;
     __QSSMAP__ = &_QSSMAP_;
     Query = new QSqlQuery(*DATABASE);

    qRegisterMetaType<QPair<bool, NT::WIN_ATTRIBUTE>>("QPair<bool, OptimizationWinQuite>");
    qRegisterMetaType<QPair<QString, bool>>("QPair<QString, bool>");
    qRegisterMetaType<QPair<NT::TASKEXEC, QVariant>>("QPair<NT::TASKEXEC, QVariant>");
    impact_int.insert(RPLKEY.UPD_THREAD_STATE, NT::THREAD_RECOVER);
    impact_swtc.insert(RPLKEY.UPD_HLIGHT_RECOVER, true);
    impact_int.insert("HighLightType",NT::CUT_GLOBALHL); //# 高亮设置默认值，默认全局高亮，如不设置，则高亮后回滚查看信息，之前已经高亮的数据恢高亮消失，原因为高亮走的关键字通道，不是全局高亮通道

    //#==========初始工具参数获取
    int ToolType = (*__RLMAP__)["TOOL"]["ToolType"].simplified().toInt();
    if (ToolType > RegisterSpToolsCount ||
        ToolType <=0)
    {
        impact_int.insert(RPLKEY.CUT_VIEW_INDEX, 1);
    }
    else
    {
        impact_int.insert(RPLKEY.CUT_VIEW_INDEX, ToolType);
    }
    diary(QString("current initialize function type: %1").arg(ToolType), Diary::Debug)
    diary(QString("current function count: %1 index: %2").arg(QString::number(RegisterSpToolsCount),
                                                                      QString::number(impact_int[RPLKEY.CUT_VIEW_INDEX])),
                                                                      Diary::Debug)

    //#Public Abstract Initialize
    TOOL_MENUADJST_QTIER.setInterval(500);
    TOOL_MENUADJST_QTIER.setSingleShot(true);
    //#Configural Module Size
    //#AnalyTop-Size
    RoofWidget->setFixedHeight(30);
    TR_actionBut->setFixedHeight(30);
    FL_fileaddBut->setFixedHeight(30);
    SP_serialBut->setFixedHeight(30);
    AS_assistBut->setFixedHeight(30);
    VW_viewcutBut->setFixedHeight(30);

    VW_rtLab->setFixedHeight(15);

    //#AnalyCentra-Size
    DT_scview->setFixedHeight(30);
    DT_remove->setFixedHeight(30);
    DT_rsview->setFixedHeight(30);
    DT_hlview->setFixedHeight(30);
    DT_exec->setFixedHeight(30);
    DT_anlyGbar->setFixedHeight(20);

    CP_compareListWidget->setMaximumWidth(100);
    CP_indexSubmit->setFixedHeight(25);

    //#SPCentra-Sizze
    SP_usbportBut->setMinimumSize(100, 30);
    SP_dtplaceBut->setMinimumSize(80, 30);
    SP_baudrateBut->setMinimumSize(80, 30);
    SP_stplaceBut->setMinimumSize(80, 30);
    SP_ckplaceBut->setMinimumSize(80, 30);
    SP_scontrolBut->setMinimumSize(80, 30);
    SP_connectSqlSaveBut->setMaximumSize(60, 25);
    SP_connectSqlAlterBut->setMaximumSize(60, 25);
    SP_commandsendBut->setMinimumHeight(25);
    SP_commandsaveBut->setMinimumSize(80, 25);
    SP_commandinputBut->setMinimumSize(80, 25);
    SP_commandsaveBut->setMaximumSize(80, 25);
    SP_commandinputBut->setMaximumSize(80, 25);
    SP_hlsubmitBut->setMinimumWidth(79);
    SP_ksearchEdit->setFixedHeight(35);
    SP_ksubmitBut->setFixedSize(80, 35);
    SP_findBackward->setFixedHeight(25);
    SP_sensiticeCheck->setFixedHeight(25);
    SP_TabWidget->setFixedHeight(86);

    SPitem_viewNikeLB->setFixedSize(60, 30);
    SPitem_viewBaudLB->setFixedSize(60, 30);
    SPitem_viewDtplLB->setFixedSize(60, 30);
    SPitem_viewStplLB->setFixedSize(60, 30);
    SPitem_viewCheckLB->setFixedSize(60, 30);
    SPitem_viewStreamLB->setFixedSize(60, 30);

    SP_portLabel->setFixedHeight(25);
    SP_baudLabel->setFixedHeight(25);
    SP_dtbitsLabel->setFixedHeight(25);
    SP_stbitsLabel->setFixedHeight(25);
    SP_ckPlaceLabel->setFixedHeight(25);
    SP_scontrolLabel->setFixedHeight(25);

    SPitem_viewWidget->setFixedSize(324, 200);
    SPitem_viewBaudV->setFixedSize(100, 30);
    SPitem_viewdataplV->setFixedSize(100, 30);
    SPitem_viewStopplV->setFixedSize(100, 30);
    SPitem_viewNikeV->setFixedSize(100, 30);
    SPitem_viewCheckV->setFixedSize(100, 30);
    SPitem_viewStreamV->setFixedSize(100, 30);

    SP_hltextEdit->setFixedHeight(29);
    SP_hlfrclEdit->setFixedHeight(29);
    SP_hlbkclEdit->setFixedHeight(29);

    //#QrcodeCentra-Size
    QR_dataEdit->setMinimumHeight(40);
    QR_keysEdit->setMinimumHeight(40);
    QR_aliasEdit->setMinimumHeight(40);
    QR_dataEdit->setMaximumHeight(60);
    QR_keysEdit->setMaximumHeight(60);
    QR_aliasEdit->setMaximumHeight(60);
    QR_hintLabel->adjustSize();
    QR_modeCutBox->setFixedWidth(100);
    QR_saveQrBox->setFixedWidth(100);

    //# QrcodeBoton-Size
    QR_saveBut->setFixedHeight(30);

    //#Configural Module StyleSheet

    diary("stylesheet load start", Diary::Debug)
    TR_actionBut->setStyleSheet((*__QSSMAP__)["anl_tbt"]);
    FL_fileaddBut->setStyleSheet((*__QSSMAP__)["anl_tbt"]);
    AS_assistBut->setStyleSheet((*__QSSMAP__)["anl_tbt"]);
    VW_viewcutBut->setStyleSheet((*__QSSMAP__)["anl_tbt"]);
    QR_saveBut->setStyleSheet((*__QSSMAP__)["anl_stdbt"]);
    SP_serialBut->setStyleSheet((*__QSSMAP__)["anl_tbt"]);
    SP_dtplaceBut->setStyleSheet((*__QSSMAP__)["anl_tbt"]);
    SP_baudrateBut->setStyleSheet((*__QSSMAP__)["anl_tbt"]);
    SP_stplaceBut->setStyleSheet((*__QSSMAP__)["anl_tbt"]);
    SP_ckplaceBut->setStyleSheet((*__QSSMAP__)["anl_tbt"]);
    SP_scontrolBut->setStyleSheet((*__QSSMAP__)["anl_tbt"]);
    SP_usbportBut->setStyleSheet((*__QSSMAP__)["anl_tbt"]);
    SP_connectSqlSaveBut->setStyleSheet((*__QSSMAP__)["anl_stdbt"]);
    SP_connectSqlAlterBut->setStyleSheet((*__QSSMAP__)["anl_stdbt"]);
    SP_usbPortMenu->setStyleSheet((*__QSSMAP__)["anl_menu"]);
    SP_baudRateMenu->setStyleSheet((*__QSSMAP__)["anl_menu"]);
    SP_dtplaceMenu->setStyleSheet((*__QSSMAP__)["anl_menu"]);
    SP_stplaceMenu->setStyleSheet((*__QSSMAP__)["anl_menu"]);
    SP_ckplaceMenu->setStyleSheet((*__QSSMAP__)["anl_menu"]);
    SP_scontrolMenu->setStyleSheet((*__QSSMAP__)["anl_menu"]);
    SPitem_viewBaudV->setStyleSheet((*__QSSMAP__)["anl_tbt"]);
    SPitem_viewdataplV->setStyleSheet((*__QSSMAP__)["anl_tbt"]);
    SPitem_viewStopplV->setStyleSheet((*__QSSMAP__)["anl_tbt"]);
    SPitem_viewNikeV->setStyleSheet((*__QSSMAP__)["anl_tbt"]);
    SPitem_viewCheckV->setStyleSheet((*__QSSMAP__)["anl_tbt"]);
    SPitem_viewStreamV->setStyleSheet((*__QSSMAP__)["anl_tbt"]);
    TR_actionMenu->setStyleSheet((*__QSSMAP__)["anl_menu"]);
    FL_actionMenu->setStyleSheet((*__QSSMAP__)["anl_menu"]);
    AS_actionMenu->setStyleSheet((*__QSSMAP__)["anl_menu"]);
    SP_configMenu->setStyleSheet((*__QSSMAP__)["anl_menu"]);
    VW_actionMenu->setStyleSheet((*__QSSMAP__)["anl_menu"]);

    SP_TabWidget->setStyleSheet((*__QSSMAP__)["anl_tabwin"]);
    SP_TabWidget->tabBar()->setStyleSheet((*__QSSMAP__)["anl_tbar"]);

    DT_scview->setStyleSheet((*__QSSMAP__)["anl_stdbt"]);
    DT_remove->setStyleSheet((*__QSSMAP__)["anl_stdbt"]);
    DT_hlview->setStyleSheet((*__QSSMAP__)["anl_stdbt"]);
    DT_rsview->setStyleSheet((*__QSSMAP__)["anl_stdbt"]);

    CP_indexSubmit->setStyleSheet((*__QSSMAP__)["anl_stdbt"]);

    SP_commandsendBut->setStyleSheet((*__QSSMAP__)["anl_stdbt"]);
    SP_commandinputBut->setStyleSheet((*__QSSMAP__)["anl_stdbt"]);
    SP_commandsaveBut->setStyleSheet((*__QSSMAP__)["anl_stdbt"]);
    SP_hlsubmitBut->setStyleSheet((*__QSSMAP__)["anl_stdbt"]);
    SP_ksubmitBut->setStyleSheet((*__QSSMAP__)["anl_stdbt"]);

    QR_modeCutBox->setStyleSheet((*__QSSMAP__)["mpl_checkbox"].arg(":/PIXMAP/bench/sgbox-ckst.png",
                                                                   ":/PIXMAP/bench/sgbox-cked.png",
                                                                   ":/PIXMAP/bench/sgbox-ckhv.png"));

    QR_saveQrBox->setStyleSheet((*__QSSMAP__)["mpl_checkbox"].arg(":/PIXMAP/bench/sgbox-ckst.png",
                                                                  ":/PIXMAP/bench/sgbox-cked.png",
                                                                  ":/PIXMAP/bench/sgbox-ckhv.png"));

    SP_newlineCheckBox->setStyleSheet((*__QSSMAP__)["mpl_checkbox"].arg(":/PIXMAP/bench/sgbox-ckst.png",
                                                                        ":/PIXMAP/bench/sgbox-cked.png",
                                                                        ":/PIXMAP/bench/sgbox-ckhv.png"));

    SP_sensiticeCheck->setStyleSheet((*__QSSMAP__)["mpl_checkbox"].arg(":/PIXMAP/bench/sgbox-ckst.png",
                                                                       ":/PIXMAP/bench/sgbox-cked.png",
                                                                       ":/PIXMAP/bench/sgbox-ckhv.png"));

    SP_findBackward->setStyleSheet((*__QSSMAP__)["mpl_checkbox"].arg(":/PIXMAP/bench/sgbox-ckst.png",
                                                                     ":/PIXMAP/bench/sgbox-cked.png",
                                                                     ":/PIXMAP/bench/sgbox-ckhv.png"));

    SP_hltextEdit->setStyleSheet((*__QSSMAP__)["mpl_uscaled"]);
    SP_hlbkclEdit->setStyleSheet((*__QSSMAP__)["mpl_uscaled"]);
    SP_hlfrclEdit->setStyleSheet((*__QSSMAP__)["mpl_uscaled"]);
    SP_ksearchEdit->setStyleSheet((*__QSSMAP__)["mpl_uscaled"]);
    SP_commandEdit->setStyleSheet((*__QSSMAP__)["mpl_uscaled"]);

    CP_compareListWidget->setStyleSheet((*__QSSMAP__)["anl_lstwin"].arg(15));
    BS_listWidget->setStyleSheet((*__QSSMAP__)["anl_lstwin"].arg(13));
    DT_exec->setStyleSheet((*__QSSMAP__)["anl_stdbt"]);
    diary("stylesheet load end", Diary::Debug)
    //#Qrcode
    diary("main layout congig start", Diary::Debug)
    //#Configural Widget Layout
    setAttribute(Qt::WA_TranslucentBackground);
    Global_TGrid->setContentsMargins(0, 0, 0, 0);
    Global_TGrid->setVerticalSpacing(0);
    Global_TGrid->addWidget(RoofWidget, 0, 0, 1, 10);
    Global_TGrid->addWidget(Active_contentWidet, 1, 0, 14, 10);

    Active_Left_Grid->setVerticalSpacing(0);
    Active_Right_Grid->setVerticalSpacing(0);
    Active_Right_Grid->setHorizontalSpacing(1);
    Active_contentGrid->setHorizontalSpacing(0);
    Active_contentGrid->setVerticalSpacing(0);

    Active_Left_Grid->setContentsMargins(0,0,0,0);
    Active_Right_Grid->setContentsMargins(0,0,0,0);
    Active_contentGrid->setContentsMargins(0,0,0,0);
    Active_contentGrid->addLayout(Active_Left_Grid, 0, 0, 14, 10);
    Active_contentGrid->addLayout(Active_Right_Grid, 0, 10, 14, 2);

    Active_Left_Grid->addWidget(PBL_centraStackWidget, 0, 0, 13, 10);
    Active_Left_Grid->addWidget(SPitem_viewWidget, 0, 9, 5, 1, Qt::AlignRight|Qt::AlignTop);
    Active_Left_Grid->addWidget(DT_anlyGbar, 10, 0, 1, 10);

    Active_Right_Grid->addLayout(&DT_multiFacehBox, 0, 0, 1, 3);
    Active_Right_Grid->addWidget(BS_listWidget, 1, 0, 12, 3);
    Active_Right_Grid->addWidget(DT_exec, 13, 0, 1, 3);

    Global_TWidget->setLayout(Global_TGrid);
    Active_contentWidet->setLayout(Active_contentGrid);
    diary("main layout congig end", Diary::Debug)

    setCentralWidget(Global_TWidget);

    d_margin_radius(QMargins(3, 0, 3, 3), 4);
    d_filler_enable(NTitleBar::BACKCOLOR, true); //可选背景色和背景图两个模式
    d_filler_color(QColor(242,241,247)); //背景色
    captionBar()->setFixedHeight(40); //设置标题栏高，默认是28
    captionBar()->d_filler_enable(NTitleBar::BACKCOLOR, true);
    captionBar()->d_filler_color(QColor(251,252,253));
    captionBar()->d_caption("zTool", QFont("PMingLiu", 10), QColor(120,120,120)); //设置标题栏文字
    captionBar()->d_captionico(":/PIXMAP/bench/scale2.ico");
    captionBar()->d_filler_enable(NTitleBar::BACKCOLOR, true); //设置标题栏填充为背景色模式
    captionBar()->d_basicbuttom_freepixmap(NTitleBar::NORWIN, 5, qRgb(49,49,49));
    captionBar()->d_basicbuttom_freepixmap(NTitleBar::MINWIN, 5, qRgb(49,49,49));
    captionBar()->d_basicbuttom_freepixmap(NTitleBar::MAXWIN, 5, qRgb(49,49,49));
    captionBar()->d_basicbuttom_freepixmap(NTitleBar::QUITE,  5, qRgb(49,49,49));
    captionBar()->d_basicbuttom_configur(NT::PIXLOAD_IGNORERATIO_LOWER, NT::DRAWTYPE_RECT, 8, 1.3);
    captionBar()->d_basicbuttom_where({NTitleBar::Rroof_Normal,
                                       NTitleBar::Rroof_Min,
                                       NTitleBar::Rroof_Max,
                                       NTitleBar::Rroof_Exit},
                                      {QString(MSEVKEY.WIN_NORMAL),
                                       QString(MSEVKEY.WIN_LEAVE),
                                       QString(MSEVKEY.WIN_PRESS),
                                       QString(MSEVKEY.WIN_RELEASE)});
    BoilPool = new DialPool(impact_thread,
                            _RGLRLSMAP_,
                            impact_str,
                            impact_swtc,
                            impact_int,
                            impact_stchar,
                            impact_stint,
                            impact_stspchar,
                            DATABASE);
    BoilPool->setMaxThreadCount(QThread::idealThreadCount());
    TOOL_MENUADJST_QTIER.start();

    diary("set windown attribute", Diary::Debug)

};


void ToolWin::SP_setActionAdjustSize(QToolButton *Button){
    QMenu *Menu = Button->menu();
    QList<QAction *> SelectionList = Menu->actions();
    QList<qint64> SelectionLenList;
    if (!SelectionList.isEmpty())
    {
        foreach(QAction *Actext, SelectionList)
        {
            QFontMetrics Metrics(Menu->font());
            SelectionLenList.append(Metrics.horizontalAdvance(Actext->text()));
        }
        qint64 maxTextSize = definfc.Max(SelectionLenList);
        uint butSize = Button->width();
        if (maxTextSize > butSize && ceil(maxTextSize / butSize) <= 3)
        {
            Menu->setFixedWidth(maxTextSize + 20);
        }
        else
        {
            Menu->setFixedWidth(butSize);
        }
    }
}



ToolWin::~ToolWin(){
    delete TR_actionMenu;
    delete FL_actionMenu;
    delete AS_actionMenu;
    delete VW_actionMenu;
    delete SP_configMenu;
    delete SP_usbPortMenu;
    delete SP_baudRateMenu;
    delete SP_dtplaceMenu;
    delete SP_stplaceMenu;
    delete SP_ckplaceMenu;
    delete SP_scontrolMenu;
    delete BS_listWidget;

    delete BoilPool;
    delete SP_sralPort;
    delete MatchKeyWord;
    delete SyntaxHLight;
    delete CloakTop;
    delete CloakCentra;
    delete CloakBotton;
    delete CloakMain;

}


